//
//  NFSComparisonViewController.m
//  NeedForSpeed
//
//  Created by 王国强 on 15/10/23.
//  Copyright © 2015年 Avenger. All rights reserved.
//

#import "NFSComparisonViewController.h"

#import "BaseHeader.h"

#import "NFSWCarsView.h"

#import "NFSWComTableViewCell.h"
@interface NFSComparisonViewController ()<UITableViewDataSource,UITableViewDelegate,giveModelDelegate>


@property(nonatomic,strong)NFSWCarsView *rightTableView;

@property(nonatomic,strong)NSMutableArray *tableViewArray;

@property(nonatomic,strong)UITableView *mainTableView;
@end

@implementation NFSComparisonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"车型对比";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    _tableViewArray = [[NSMutableArray alloc]init];
    [self cteateTableView];
    [self createTabBarView];
    [self createRightTableView];

}
- (void)cteateTableView
{
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height) style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource =self;
    [self.view addSubview:_mainTableView];
}
- (void)createRightTableView
{
    _rightTableView = [[NFSWCarsView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH  , 64, SCREEN_WIDTH , SCREEN_HEIGHT)];
    [self.view addSubview:_rightTableView];
    
    UISwipeGestureRecognizer *rightTablePush = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightPush)];
    [_rightTableView addGestureRecognizer:rightTablePush];
}
- (void)rightPush
{
    [UIView animateWithDuration:0.5 animations:^{
        
        [_rightTableView setFrame:CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT)];
    }];
}
- (void)createTabBarView
{
    UIView *tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height, SCREEN_WIDTH, self.tabBarController.tabBar.frame.size.height )];
    [tabBarView setBackgroundColor:[UIColor colorWithRed:247. / 255. green:247./255. blue:247./255. alpha:1]];
    [self.view addSubview:tabBarView];
    
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 4 / 5, 0, SCREEN_WIDTH / 10, self.tabBarController.tabBar.frame.size.height)];
    [addButton setTitle:@"添加" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addTableView) forControlEvents:UIControlEventTouchUpInside];
    [tabBarView addSubview:addButton];

}
- (void)addTableView
{
    [_rightTableView setFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _rightTableView.CarsGiveDelegate =self;
}
- (void)giveModel:(NFSVehicleModel *)model
{
    [_tableViewArray  addObject:model];
    [_mainTableView reloadData];
    [UIView animateWithDuration:0.5 animations:^{
        
        [_rightTableView setFrame:CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT)];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableViewArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    NFSWComTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[NFSWComTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NFSVehicleModel *model = [_tableViewArray objectAtIndex:indexPath.row];
    [cell.nameLabel setText:[NSString stringWithFormat:@"车型: %@",model.name]];
     
    [cell.priceLabel setText:[NSString stringWithFormat:@"价格: %@",model.price]];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_mainTableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    [_tableViewArray removeObjectAtIndex:indexPath.row];
    [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除车型";
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_tableViewArray removeObjectAtIndex:sourceIndexPath.row];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
